/*
 (C) Copyright MarketLive. 2006. All rights reserved.
 MarketLive is a trademark of MarketLive, Inc.
 Warning: This computer program is protected by copyright law and international treaties.
 Unauthorized reproduction or distribution of this program, or any portion of it, may result
 in severe civil and criminal penalties, and will be prosecuted to the maximum extent
 possible under the law.
 */

package com.marketlive.entity.account;


import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.marketlive.entity.IEntity;
import org.marketlive.entity.IPrimaryKey;
import org.marketlive.entity.account.IAccountSet;
import org.marketlive.entity.account.IAccountSetHome;

import com.marketlive.entity.EntityHome;
import com.marketlive.entity.IntegerPk;

/**
 * @inheritDoc
 */
public class AccountSetHome extends EntityHome implements IAccountSetHome {

    /**
     * The LogFactory of the AccountSet class.
     */
    private static Log log = LogFactory.getLog(AccountSetHome.class);

    /**
     * Constructor: Instantiate an AccountSet Home Object.
     * 
     * @throws Exception
     *             if can not create a AccountSet home object
     */
    public AccountSetHome() throws Exception {
        super(AccountSet.ENTITY_TYPE, AccountSet.class);
        AccountSet.setEntityHome(this);
    }

    /**
     * {@inheritDoc}
     */
    public IPrimaryKey getPk(final String pkString) {
        int pkInt = -1;
        try {
            pkInt = Integer.parseInt(pkString);
        } catch (NumberFormatException e) {
            throw new RuntimeException(
                    "pkString for AccountSet must be an integer.");
        }
        return new IntegerPk("accountSet", pkInt);
    }

    /**
     * {@inheritDoc}
     */
    public IEntity get(final IPrimaryKey pk) {
        AccountSet a = new AccountSet();
        a.setPk(pk);
        return a;
    }

    /**
     * {@inheritDoc}
     */
    public IAccountSet create(final String code, final String name) {
        AccountSet set = new AccountSet(code, name);
        super.create(set);
        return (IAccountSet) set;
    }

    /**
     * {@inheritDoc}
     */
    public IAccountSet findByCode(final String code) {
        List accountSets = getHibernateTemplate().find("from AccountSet where code = ?", code);
        if (accountSets.isEmpty()) {
            return null;
        }
        // no need to check for uniqueness - account sets have a constraint on the "code" field
        return (IAccountSet)accountSets.get(0);
    }

}
